package com.neu.canteen.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neu.canteen.dao.FoodDao;
import com.neu.canteen.domain.Food;
import com.neu.canteen.domain.FoodType;
import com.neu.canteen.utils.JdbcUtil;

public class FoodDaoImpl implements FoodDao{
 
	@Override
	public List<Food> findByTypeId(int typeId) {

		List<Food> foods = new ArrayList<Food>();
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			conn = JdbcUtil.getConn();
			
			String sql = "select *from food";
			if(typeId != -1){
				sql = sql+" where food_type_id = ?";
			}
			preparedStatement = conn.prepareStatement(sql);
			
			if(typeId != -1){
				preparedStatement.setInt(1, typeId);
			}
			
			resultSet = preparedStatement.executeQuery();

			
			Food food = null;
			while(resultSet.next()){
				food = new Food();
				food.setId(resultSet.getInt("id"));
				food.setName(resultSet.getString("name"));
				food.setMaterial(resultSet.getString("material"));
				food.setRemark(resultSet.getString("remark"));
				food.setPrice(resultSet.getInt("price"));
				food.setPackagingRate(resultSet.getInt("packaging_rate"));
				 
				
				
				foods.add(food);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(resultSet,preparedStatement,conn);
		}
		return foods;
	}
	@Override
	public Food findById(int foodId) {
		Food food = null;
		
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			conn = JdbcUtil.getConn();
			
			String sql = "select *from food where id = ?";
			 
			preparedStatement = conn.prepareStatement(sql);
			 
			preparedStatement.setInt(1, foodId);
			 
			
			resultSet = preparedStatement.executeQuery(); 
			if(resultSet.next()){
				food = new Food();
				food.setId(resultSet.getInt("id"));
				food.setName(resultSet.getString("name"));
				food.setMaterial(resultSet.getString("material"));
				food.setRemark(resultSet.getString("remark"));
				food.setPrice(resultSet.getInt("price"));
				food.setPackagingRate(resultSet.getInt("packaging_rate"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(resultSet,preparedStatement,conn);
		}
		return food;
	}
	
	
	public int add(Food food){
		Connection conn=null;
		PreparedStatement preparedStatement=null;
		try {
			conn=JdbcUtil.getConn();
			String sql="insert into food(name,price,packaging_rate,img,food_type_id,remark,material,status) value (?,?,?,?,?,?,?,?)";
			preparedStatement=conn.prepareStatement(sql);
			preparedStatement.setString(1,food.getName());
			preparedStatement.setInt(2, food.getPrice());
			preparedStatement.setInt(3, food.getPackagingRate());
			preparedStatement.setString(4, food.getImg());
			preparedStatement.setInt(5, food.getFoodTypeId());
			preparedStatement.setString(6, food.getRemark());
			preparedStatement.setString(7, food.getMaterial());
			preparedStatement.setInt(8, food.getStatus());
			
			return preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			JdbcUtil.close(null, preparedStatement, conn);
		}
		return -1;
	}
	
	@Override
	public int deleteFoodId(int id) {
		 
		Connection conn =null;
		PreparedStatement preparedStatement=null;
		try {
			conn = JdbcUtil.getConn();
			
			String sql = "delete from food where id = ?";
			preparedStatement = conn.prepareStatement(sql);
			
			preparedStatement.setInt(1, id);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(null, preparedStatement, conn);
		}
		return -1;
	}
	
	public int updataFood(Food food){
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		try {
			connection=JdbcUtil.getConn();
			String sql=("update food set name=?,price=?,packaging_rate=?,img=?,food_type_id=?,remark=?,material=?,status=? where id=?" );
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1,food.getName());
			preparedStatement.setInt(2, food.getPrice());
			preparedStatement.setInt(3, food.getPackagingRate());
			preparedStatement.setString(4, food.getImg());
			preparedStatement.setInt(5, food.getFoodTypeId());
			preparedStatement.setString(6, food.getRemark());
			preparedStatement.setString(7, food.getMaterial());
			preparedStatement.setInt(8, food.getStatus());
			preparedStatement.setInt(9, food.getId());
			return preparedStatement.executeUpdate();
		} catch (Exception e) {
			
			e.printStackTrace();
			// TODO: handle exception
		}finally {
			JdbcUtil.close(null, preparedStatement, connection);
		}
		return -1;
		
	}
}